home *** CD-ROM | disk | FTP | other *** search
/ The CICA Windows Explosion! / The CICA Windows Explosion! - Disc 2.iso / misc / gs261src.zip / gdevpcl.c < prev    next >
C/C++ Source or Header  |  1993-05-13  |  8KB  |  236 lines

  1. /* Copyright (C) 1992 Aladdin Enterprises.  All rights reserved.
  2.  
  3. This file is part of Ghostscript.
  4.  
  5. Ghostscript is distributed in the hope that it will be useful, but
  6. WITHOUT ANY WARRANTY.  No author or distributor accepts responsibility
  7. to anyone for the consequences of using it or for whether it serves any
  8. particular purpose or works at all, unless he says so in writing.  Refer
  9. to the Ghostscript General Public License for full details.
  10.  
  11. Everyone is granted permission to copy, modify and redistribute
  12. Ghostscript, but only under the conditions described in the Ghostscript
  13. General Public License.  A copy of this license is supposed to have been
  14. given to you along with Ghostscript so you can know your rights and
  15. responsibilities.  It should be in a file named COPYING.  Among other
  16. things, the copyright notice and this notice must be preserved on all
  17. copies.  */
  18.  
  19. /* gdevpcl.c */
  20. /* Utilities for PCL printers */
  21. #include "gdevprn.h"
  22. #include "gdevpcl.h"
  23.  
  24. /* ------ Get paper size ------ */
  25.  
  26. /* Get the paper size code, based on width and height. */
  27. int
  28. gdev_pcl_paper_size(gx_device *dev)
  29. {    return
  30.       (dev->height / dev->y_pixels_per_inch >= 16.0 ? PAPER_SIZE_A3 :
  31.        dev->height / dev->y_pixels_per_inch >= 11.8 ? PAPER_SIZE_LEGAL :
  32.        dev->height / dev->y_pixels_per_inch >= 11.1 ? PAPER_SIZE_A4 :
  33.        PAPER_SIZE_LETTER);
  34. }
  35.  
  36. /* ------ Get initial matrix ------ */
  37.  
  38. /* Get an initial matrix adjusted for the margins. */
  39.  
  40. /*
  41.  * PCL has the notion of a LOGICAL and a PHYSICAL page.  The PHYSICAL page is
  42.  * the actual paper; the LOGICAL page is the printer specific imageable area.
  43.  * The strange part is that coordinates are all relative to the
  44.  * LOGICAL page.  This means that all PCL code is inherently device dependent.
  45.  * Luckily, in PostScript land, transformation matrices conquer all.
  46.  */
  47.  
  48. void
  49. gdev_pcl_get_initial_matrix(gx_device *dev, gs_matrix *pmat)
  50. {       pmat->xx = dev->x_pixels_per_inch / 72.0;
  51.     pmat->xy = 0;
  52.     pmat->yx = 0;
  53.     pmat->yy = dev->y_pixels_per_inch / -72.0;
  54.     pmat->tx = -(dev->l_margin * dev->x_pixels_per_inch);
  55.     pmat->ty = dev->height - (dev->t_margin * dev->y_pixels_per_inch);
  56. }
  57.  
  58. /* ------ Color mapping ------ */
  59.  
  60. /* The PaintJet and DeskJet 500C use additive colors in separate planes. */
  61. /* We only keep one bit of color, with 1 = R, 2 = G, 4 = B. */
  62. /* Because the buffering routines assume 0 = white, */
  63. /* we complement all the color components. */
  64. #define cv_shift (sizeof(gx_color_value) * 8 - 1)
  65.  
  66. /* Map an RGB color to a printer color. */
  67. gx_color_index
  68. gdev_pcl_3bit_map_rgb_color(gx_device *dev,
  69.   gx_color_value r, gx_color_value g, gx_color_value b)
  70. {    return (((b >> cv_shift) << 2) + ((g >> cv_shift) << 1) + (r >> cv_shift)) ^ 7;
  71. }
  72.  
  73. /* Map the printer color back to RGB. */
  74. int
  75. gdev_pcl_3bit_map_color_rgb(gx_device *dev, gx_color_index color,
  76.   gx_color_value prgb[3])
  77. {    ushort cc = (ushort)color ^ 7;
  78.     prgb[0] = -(cc & 1);
  79.     prgb[1] = -((cc >> 1) & 1);
  80.     prgb[2] = -(cc >> 2);
  81.     return 0;
  82. }
  83.  
  84. /* ------ Compression ------ */
  85.  
  86. /*
  87.  * Mode 2 Row compression routine for the HP DeskJet & LaserJet IIp.
  88.  * Compresses data from row up to end_row, storing the result
  89.  * starting at compressed.  Returns the number of bytes stored.
  90.  * Runs of K<=127 literal bytes are encoded as K-1 followed by
  91.  * the bytes; runs of 2<=K<=127 identical bytes are encoded as
  92.  * 257-K followed by the byte.
  93.  * In the worst case, the result is N+(N/127)+1 bytes long,
  94.  * where N is the original byte count (end_row - row).
  95.  * To speed up the search, we examine an entire word at a time.
  96.  * We will miss a few blocks of identical bytes; tant pis.
  97.  */
  98. int
  99. gdev_pcl_mode2compress(const word *row, const word *end_row, char *compressed)
  100. {    register const word *exam = row; /* word being examined in the row to compress */
  101.     register char *cptr = compressed; /* output pointer into compressed bytes */
  102.  
  103.     while ( exam < end_row )
  104.        {    /* Search ahead in the input looking for a run */
  105.         /* of at least 4 identical bytes. */
  106.         const char *compr = (const char *)exam;
  107.         const char *end_dis;
  108.         const word *next;
  109.         register word test;
  110.         while ( exam < end_row )
  111.           { test = *exam;
  112.             if ( ((test << 8) ^ test) <= 0xff )
  113.               break;
  114.             exam++;
  115.           }
  116.  
  117.         /* Find out how long the run is */
  118.         end_dis = (const char *)exam;
  119.         if ( exam == end_row )    /* no run */
  120.           { /* See if any of the last 3 "dissimilar" bytes are 0. */
  121.             if ( end_dis > compr && end_dis[-1] == 0 )
  122.               { if ( end_dis[-2] != 0 ) end_dis--;
  123.             else if ( end_dis[-3] != 0 ) end_dis -= 2;
  124.             else end_dis -= 3;
  125.               }
  126.             next = --end_row;
  127.           }
  128.         else
  129.           { next = exam + 1;
  130.             while ( next < end_row && *next == test )
  131.               next++;
  132.             /* See if any of the last 3 "dissimilar" bytes */
  133.             /* are the same as the repeated byte. */
  134.             if ( end_dis > compr && end_dis[-1] == (byte)test )
  135.               { if ( end_dis[-2] != (byte)test ) end_dis--;
  136.             else if ( end_dis[-3] != (byte)test ) end_dis -= 2;
  137.             else end_dis -= 3;
  138.               }
  139.           }
  140.  
  141.         /* Now [compr..end_dis) should be encoded as dissimilar, */
  142.         /* and [end_dis..next) should be encoded as similar. */
  143.         /* Note that either of these ranges may be empty. */
  144.  
  145.         for ( ; ; )
  146.            {    /* Encode up to 127 dissimilar bytes */
  147.             uint count = end_dis - compr; /* uint for faster switch */
  148.             switch ( count )
  149.               { /* Use memcpy only if it's worthwhile. */
  150.               case 6: cptr[6] = compr[5];
  151.               case 5: cptr[5] = compr[4];
  152.               case 4: cptr[4] = compr[3];
  153.               case 3: cptr[3] = compr[2];
  154.               case 2: cptr[2] = compr[1];
  155.               case 1: cptr[1] = compr[0];
  156.                 *cptr = count - 1;
  157.                 cptr += count + 1;
  158.               case 0: /* all done */
  159.                 break;
  160.               default:
  161.                 if ( count > 127 ) count = 127;
  162.                 *cptr++ = count - 1;
  163.                 memcpy(cptr, compr, count);
  164.                 cptr += count, compr += count;
  165.                 continue;
  166.               }
  167.             break;
  168.            }
  169.  
  170.            {    /* Encode up to 127 similar bytes. */
  171.             /* Note that count may be <0 at end of row. */
  172.             int count = (const char *)next - end_dis;
  173.             while ( count > 0 )
  174.               { int this = (count > 127 ? 127 : count);
  175.                 *cptr++ = 257 - this;
  176.                 *cptr++ = (byte)test;
  177.                 count -= this;
  178.               }
  179.             exam = next;
  180.            }
  181.        }
  182.     return (cptr - compressed);
  183. }
  184.  
  185. /*
  186.  * Mode 3 compression routine for the HP LaserJet III family.
  187.  * Compresses bytecount bytes starting at current, storing the result
  188.  * in compressed, comparing against and updating previous.
  189.  * Returns the number of bytes stored.  In the worst case,
  190.  * the number of bytes is bytecount+(bytecount/8)+1.
  191.  */
  192. int
  193. gdev_pcl_mode3compress(int bytecount, const char *current, char *previous, char *compressed)
  194. {    register const char *cur = current;
  195.     register char *prev = previous;
  196.     register char *out = compressed;
  197.     const char *end = current + bytecount;
  198.     while ( cur < end )
  199.        {    /* Detect a maximum run of unchanged bytes. */
  200.         const char *run = cur;
  201.         register const char *diff;
  202.         const char *stop;
  203.         int offset, cbyte;
  204.         while ( cur < end && *cur == *prev )
  205.            {    cur++, prev++;
  206.            }
  207.         if ( cur == end ) break;    /* rest of row is unchanged */
  208.         /* Detect a run of up to 8 changed bytes. */
  209.         /* We know that *cur != *prev. */
  210.         diff = cur;
  211.         stop = (end - cur > 8 ? cur + 8 : end);
  212.         do
  213.            {    *prev++ = *cur++;
  214.            }
  215.         while ( cur < stop && *cur != *prev );
  216.         /* Now [run..diff) are unchanged, and */
  217.         /* [diff..cur) are changed. */
  218.         /* Generate the command byte(s). */
  219.         offset = diff - run;
  220.         cbyte = (cur - diff - 1) << 5;
  221.         if ( offset < 31 )
  222.             *out++ = cbyte + offset;
  223.         else
  224.            {    *out++ = cbyte + 31;
  225.             offset -= 31;
  226.             while ( offset >= 255 )
  227.                 *out++ = 255, offset -= 255;
  228.             *out++ = offset;
  229.            }
  230.         /* Copy the changed data. */
  231.         while ( diff < cur )
  232.             *out++ = *diff++;
  233.        }
  234.     return out - compressed;
  235. }
  236.